package com.hrt.manage.product.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.manage.product.dao.GiftInforDao;
import com.hrt.manage.product.service.GiftInforService;


@Transactional
@Service("giftInforService")
public class GiftInforServiceImpl implements GiftInforService{
	@Autowired
	private GiftInforDao giftInfordao; 
	
	/**
	 * 保存实体
	 * */
	
	@Override
	public void addGiftInfor(GiftInfor giftInfor) {
		this.giftInfordao.saveEntity(giftInfor);
	}

	/**
	 * 更新实体
	 * */
	
	@Override
	public void updateGiftInfor(GiftInfor giftInfor) {
		this.giftInfordao.updateEntity(giftInfor);
	}

	/**
	 * 删除实体
	 * */
	
	@Override
	public void delGiftInfor(Long id) {
		this.giftInfordao.deleteEntity(id);
	}

	/**
	 * 按id查询实体
	 * */
	@Override
	public GiftInfor getById(Long id) {
		return this.giftInfordao.getById(id);
	}

	/**
	 * 查询所有
	 * */
	@Override
	public List<GiftInfor> searchAll() {
		return this.giftInfordao.findAll();
	}

	/**
	 * 按是否删除查询
	 */
	@Override
	public List<GiftInfor> searchDelAll() {
		return this.giftInfordao.findDelAll();
	}
	
	/**
	 * 按id组列表查询实体
	 * */
	@Override
	public List<GiftInfor> getByIds(Long[] ids) {
		return this.giftInfordao.getByIds(ids);
	}
	
	/**分页*/
	@Override
	public Long totalCount() {
		return this.giftInfordao.totalCount();
	}

	@Override
	public List<GiftInfor> queryByPage(int pageIndex, int pageSize) {
		return this.giftInfordao.queryByPage(pageIndex, pageSize);
	}

	/**获取数据库的当前Date形式的时间*/
	@Override
	public Timestamp getDate() {
		return this.giftInfordao.getDate();
	}
	
	/**删除选中的数据*/
	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<GiftInfor> delResult = giftInfordao.getByIds(ids);
		for (GiftInfor  gi: delResult) {
			gi.setIsdelete(new Integer(1));
			giftInfordao.updateEntity(gi);
		}
		
	}
	
	/**上架*/
	@Override
	public void shelveById(Long[] ids) throws Exception {
		List<GiftInfor> delResult = giftInfordao.getByIds(ids);
		for (GiftInfor  gi: delResult) {
			gi.setIspush(new Integer(1));
			giftInfordao.updateEntity(gi);
		}
	}
	
	/**下架*/
	@Override
	public void unShelveById(Long[] ids) throws Exception {
		List<GiftInfor> delResult = giftInfordao.getByIds(ids);
		for (GiftInfor  gi: delResult) {
			gi.setIspush(new Integer(0));
			giftInfordao.updateEntity(gi);
		}
	}

	/* 
	 * 模糊查询礼品
	 */
	@Override
	public List<GiftInfor> queryGiftInfors(String itemCondition) {
		List<GiftInfor> list = new ArrayList<GiftInfor>();
		list = giftInfordao.queryGiftInfors(itemCondition);
		return list;
	}
	
	/**
	 * 根据giftId查询礼品规格
	 */
	@Override
	public List<GiftDetail> getGiftDetailByGiftId(Long giftId) {
		return this.giftInfordao.getGiftDetailByGiftId(giftId);
	}
	
	/**
	 * 根据礼品ID 加载所有礼品规格（不分上下架）
	 * @param giftId
	 * @return
	 * 
	 */
	@Override
	public List<GiftDetail> getGiftDetailsByGiftId(Long giftId) {
		return this.giftInfordao.getGiftDetailsByGiftId(giftId);
	}

	@Override
	public Long computeTotal(GiftInfor giftInfor) {
		List<GiftDetail> detail=giftInfordao.getGiftDetailByGiftId(giftInfor.getId());
		Long totalSum=0l;
		for(GiftDetail d:detail)
		{
			if(d!=null)
			{
				totalSum=totalSum+d.getTotals();
			}
		}
		giftInfor.setTotals(totalSum);
		giftInfordao.updateEntity(giftInfor);
		return giftInfor.getTotals();
	}


}
